package com.gcsoftware.gateway.GWImpl;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yinpeng on 18/1/22.
 */
public class MaskerReplace4JSON
{
    private static HashMap<String, Object> replaceContent(String name, HashMap<String, Object> input)
    {
        for (HashMap.Entry<String, Object> entry : input.entrySet())
        {
            if(entry.getValue() instanceof Map)
            {
                replaceContent(name, ((HashMap) entry.getValue()));
            }
            if((entry.getValue() instanceof String)
                    && name.equals(entry.getKey()))
            {
                entry.setValue("******");
            }
        }

        return input;
    }

    public static  String MaskerReplace(String input)
            throws Exception
    {
        try
        {
            ObjectMapper obm = new ObjectMapper();
            HashMap<String, Object> map = obm.readValue(input, HashMap.class);
            map = replaceContent("c_tenant_name", map);
            //System.out.println("======replace :" + map);

            String dest = obm.writeValueAsString(map);
            return dest;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return input;
        }
    }
}
